package com.runa.rccprotocol.dto.lanjier;

import com.runa.protocol.dto.DtuRequestData;
import com.runa.protocol.dto.IRequestData;
import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.Crc32TJSW;
import com.runa.rccprotocol.dto.zhongrun.ZhongRunEquRequestData;

/**
 * @author diandian
 * @Date 2022/5/7 9:07
 */
public class FmuLanJiErRequestData extends DtuRequestData implements IRequestData<byte[]> {

    public FmuLanJiErRequestData(String dtuID, IRequestData<byte[]> requestData) {
        super(dtuID, requestData);
    }

    private byte[] fmuRequestData = null;

    private byte port = 0;

    public void setPort(byte port) {
        this.port = port;
    }

    @Override
    public byte[] getData() {
        byte[] data = requestData.getData();
        int dataLength = data.length;
        fmuRequestData = new byte[dataLength + 22];

        fmuRequestData[0] = 0x49;
        fmuRequestData[1] = 0x4F;
        fmuRequestData[2] = 0x54;
        fmuRequestData[3] = 0x40;

        //长度
        byte[] fmuDataLen = ByteUtil.shortToByte((short) (data.length + 16));
        fmuRequestData[4] = fmuDataLen[1];
        fmuRequestData[5] = fmuDataLen[0];
        //固定
        fmuRequestData[6] = 0;
        //集中器号
        ByteUtil.tenstringToBcd(fmuRequestData, this.dtuID, 7, true);

        fmuRequestData[11] = (byte) 0xD1;
        fmuRequestData[12] = (byte) 0xA2;

        byte[] dataLen = ByteUtil.shortToByte((short) (data.length + 1));
        fmuRequestData[13] = dataLen[1];
        fmuRequestData[14] = dataLen[0];

        //通道号
        fmuRequestData[15] = port;

        System.arraycopy(data, 0, fmuRequestData, 16, dataLength);

        Crc32TJSW crc32TJSW = new Crc32TJSW();
        byte[] bytesFlip = crc32TJSW.getBytesFlip(fmuRequestData, 4, 12 + dataLength);
        //crc32校验
        fmuRequestData[16 + dataLength] = bytesFlip[0];
        fmuRequestData[17 + dataLength] = bytesFlip[1];
        fmuRequestData[18 + dataLength] = bytesFlip[2];
        fmuRequestData[19 + dataLength] = bytesFlip[3];
        fmuRequestData[dataLength + 20] = 0x25;
        fmuRequestData[dataLength + 21] = 0x5E;

        //重置port
//        this.port = 0;
        return fmuRequestData;
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }


    public static void main(String[] args) {
        //中润    49 4F 54 40 00 24 00 10 46 05 21 D1 A2 00 15 01 FE FE FE FE 68 20 24 12 02 20 00 11 11 01 03 1F 90 00 B5 16 9C 01 5B D9 25 5E
        String fmu = "10460521";
        String equ = "20021224";
        byte port = 1;
        ZhongRunEquRequestData data = new ZhongRunEquRequestData(fmu);

        //力创    49 4F 54 40 00 24 00 10 34 02 48 D1 A2 00 15 01 FE FE FE FE 68 20 04 04 66 30 00 59 42 01 03 90 1F 00 74 16 AA 86 56 9E 25 5E
/*        String fmu = "10340248";
        String equ = "30660404";
        byte port = 1;
        LiChuangEquRequestData data = newComm LiChuangEquRequestData(fmu);*/
        //瑞纳    49 4F 54 40 00 24 00 10 07 06 27 D1 A2 00 15 01 FE FE FE FE 68 20 16 02 80 21 00 11 11 01 03 1F 90 00 16 16 2E 43 6A 56 25 5E
/*        String fmu = "10070627";
        String equ = "21800216";
        byte port = 1;
        RunaRCRequestData data = newComm RunaRCRequestData();*/

        //兰吉尔   49 4F 54 40 00 21 00 10 46 04 94 D1 A2 00 12 01 68 0B 0B 68 53 FD 52 27 14 20 67 FF FF FF FF 60 16 B1 62 29 5D 25 5E
        //        49 4F 54 40 00 15 00 10 46 04 94 D1 A2 00 06 01 10 5B FD 58 16 0B 51 DC B1 25 5E
/*        String fmu = "10460494";
        String equ = "67201427";
        byte port = 1;
        RunaRCRequestData data = newComm RunaRCRequestData();*/


        FmuLanJiErRequestData requestData = new FmuLanJiErRequestData(fmu, data);
        requestData.port = port;
//        requestData.setRequestData(data);
        data.readHeatMeter(equ);
        requestData.getData();

        String s = ("00 57 5F 10 46 04 94 D2 A2 00 48 FE FE FE FE FE FE FE FE FE FE FE FE FE 68 25 84 15 80 21 00 4D 22 81 2E 1F 90 00 00 00 00 00 05 48 05 00 00 05 00 00" +
                " 00 00 17 00 00 00 00 35 26 00 00 00 2C 98 19 00 63 19 00 85 66 00 38 21 00 05 05 22 20 00 80 C6 16")
                .replace(" ", "");
        byte[] bytes = ByteUtil.hexStringToByte(s);
        Crc32TJSW tjsw = new Crc32TJSW();
        byte[] bytesFlip = tjsw.getBytesFlip(bytes, 0, bytes.length);
        System.out.println(ByteUtil.byteToHexString(bytesFlip));

    }



}